package com.example.demo.task.controller;

import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.task.ServiceManager;
import com.example.demo.task.domain.MobileTask;

@RestController
@RequestMapping(value = "/mobile/task")
public class MobileTaskController {

	/**
	 * 查询一个
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/findById/{id}")
	public MobileTask findTaskById(@PathVariable Long id) {
		MobileTask task = ServiceManager.taskService.findById(id);		
		return task;
	}


	/**
	 * 发布任务
	 * @param task
	 * @param bindingResult
	 */
	@PostMapping("/create")
	public void create(@Valid MobileTask task, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			System.out.println(bindingResult.getFieldError().getDefaultMessage());
			return;
		}
		ServiceManager.taskService.save(task);
	}
	
	@PostMapping("/insert")
	public void insert(MobileTask task) {
		ServiceManager.taskService.save(task);
	}
}
